injectJsonInstruction

fun injectJsonInstruction(prompt: String? = null, schema: JsonElement? = null, schemaPrefix: String? = if (schema != null) DEFAULT_SCHEMA_PREFIX else null, schemaSuffix: String? = if (schema != null) DEFAULT_SCHEMA_SUFFIX else DEFAULT_GENERIC_SUFFIX): String

Build the JSON instruction block.

Mirrors the v6 array-filter-join exactly:

[
prompt (only if non-empty),
"" (blank separator line — only if prompt non-empty),
schemaPrefix,
JSON.stringify(schema) (only if schema present),
schemaSuffix,
].filter(notNull).join("\n")

schema.toString() on a kotlinx JsonElement yields compact JSON (no insignificant whitespace), matching JS JSON.stringify(schema).

Parameters

prompt

existing instruction text to lead with (the model's system prompt, typically). Empty / null contributes nothing and suppresses the blank separator line.

schema

the JSON schema the answer must satisfy. When null the suffix degrades to the generic "answer with JSON" form and no schema block is emitted.

schemaPrefix

label printed before the schema. Defaults to DEFAULT_SCHEMA_PREFIX when a schema is present, else null.

schemaSuffix

closing instruction. Defaults to the schema-aware suffix when a schema is present, else the generic one.